﻿using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace FileTransfer.Controls
{
    /// <summary>
    /// 带进度单元格。单元格内容格式为：值/总值 Eric
    /// </summary>
    [Description("带进度单元格。单元格内容格式为：值/总值")]
    public class DataGridViewProcessCell : DataGridViewCell
    {
        /// <summary>
        /// 进度条景色
        /// </summary>
        protected Color _ProcessColor = Color.FromArgb(64, 192, 192);

        public DataGridViewProcessCell()
        {

        }
        public DataGridViewProcessCell(Color processBackColor)
            : base()
        {
            _ProcessColor = processBackColor;
        }

        protected override object GetFormattedValue(object value,
        int rowIndex, ref DataGridViewCellStyle cellStyle,
        TypeConverter valueTypeConverter,
        TypeConverter formattedValueTypeConverter,
        DataGridViewDataErrorContexts context)
        {
            return _ProcessColor;
        }

        protected override void Paint(Graphics graphics, Rectangle clipBounds,
            Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState,
            object value, object formattedValue, string errorText,
            DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle,
            DataGridViewPaintParts paintParts)
        {
            using (SolidBrush backBrush = new SolidBrush(cellStyle.BackColor))
            {
                //维制单元格背景
                graphics.FillRectangle(backBrush, cellBounds);
            }
            base.PaintBorder(graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle);

            float percent = 0F;
            if (value != null)
            {
                string[] resultAndTotal = value.ToString().Split('/');
                if (resultAndTotal.Count() == 2)
                {
                    float result = 0;
                    float total = 0;
                    if (!float.TryParse(resultAndTotal[0], out result))
                    {
                        percent = 0;
                    }
                    else if (!float.TryParse(resultAndTotal[1], out total))
                    {
                        percent = 0;
                    }
                    else
                    {
                        percent = result / total;
                    }
                }
            }
            using (SolidBrush foreBrush = new SolidBrush(_ProcessColor))
            {
                //绘制进度图
                graphics.FillRectangle(foreBrush, cellBounds.X, cellBounds.Y + cellBounds.Height / 4, cellBounds.Width * percent, cellBounds.Height / 2);
            }
            using (SolidBrush residueBrush = new SolidBrush(Color.LightGray))
            {
                //绘进度条余下部分
                graphics.FillRectangle(residueBrush, cellBounds.X + cellBounds.Width * percent, cellBounds.Y + cellBounds.Height / 4, cellBounds.Width * (1 - percent), cellBounds.Height / 2);
            }
            string cellText = value == null ? 0.ToString() : value.ToString();
            //string cellText = value.ToString();
            SizeF sf = graphics.MeasureString(cellText, cellStyle.Font);
            float x = cellBounds.X + (cellBounds.Width - sf.Width) / 2f;
            float y = cellBounds.Y + (cellBounds.Height - sf.Height) / 2f;
            //维制单元格文本
            graphics.DrawString(cellText, cellStyle.Font, new SolidBrush(cellStyle.ForeColor), x, y);

        }
    }
}
